package com.ebt.m.commons.buscomponent.listview;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.ebt.m.commons.R;
import com.ebt.m.commons.buscomponent.listview.pulltorefresh.PullToRefreshView;
import com.ebt.m.commons.model.data.BaseDataResult;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.android.RxLifecycleAndroid;

import java.util.ArrayList;
import java.util.List;


public abstract class BaseListView<P extends BaseListPresenter> extends FrameLayout implements BaseListContract.IView, RecyclerFootManger.LoadNextListener, PullToRefreshView.OnRefreshListener {

    protected RecyclerView mRecyclerView;
    protected BaseRecyclerAdapter mLineAdpater;
    protected PullToRefreshView mRefreshLayout;
    protected P mPresenter;
    protected RecyclerFootManger footManger;
    protected RelativeLayout mProgressBar;

    public BaseListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseListView(Context context) {
        this(context, null);
    }

    public BaseListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        inflate(context, R.layout.layout_base_list, this);
        initView();
        mPresenter = createPresenter();
    }

    private void initView() {
        mRecyclerView = findViewById(R.id.content_view);
        mProgressBar = findViewById(R.id.progress_bar);
        mLineAdpater = new BaseRecyclerAdapter(getContext(), new ArrayList<SectionItem>(), createViewFactory());
        mLineAdpater.setEmptyView(configEmptyView());
        mLineAdpater.setNetErrorView(configNetErrorView());
        mLineAdpater.setErrorView(configErrorView());
        mRecyclerView.setLayoutManager(generateLayoutManager());
        mRecyclerView.setAdapter(mLineAdpater);
        if (createFootView()) {
            footManger = new RecyclerFootManger(getContext(), mRecyclerView);
            footManger.setLoadNextListner(this);
        }

        //init refresh layout
        mRefreshLayout = findViewById(R.id.refresh_layout);
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setRefreshEnable(false);
        //设置recyclerview背景色
        mRecyclerView.setBackgroundColor(setListviewColor());
    }

    protected int setListviewColor() {
        return Color.WHITE;
    }

    protected View configEmptyView() {
        View emptyContent = LayoutInflater.from(getContext()).inflate(R.layout.layout_common_empty, null);
        return emptyContent;
    }

    protected View configNetErrorView() {
        View netErrorView = LayoutInflater.from(getContext()).inflate(R.layout.layout_net_error_view, null);
        netErrorView.findViewById(R.id.load_refresh_net).setOnClickListener(view -> {
            mRefreshLayout.setRefreshing(false);
            mProgressBar.setVisibility(View.VISIBLE);
            mPresenter.refresh();
        });
        return netErrorView;
    }

    protected View configErrorView() {
        View netErrorView = LayoutInflater.from(getContext()).inflate(R.layout.layout_error_view, null);
        netErrorView.findViewById(R.id.load_refresh).setOnClickListener(view -> {
            mRefreshLayout.setRefreshing(false);
            mProgressBar.setVisibility(View.VISIBLE);
            mPresenter.refresh();
        });
        return netErrorView;
    }

    protected RecyclerView.LayoutManager generateLayoutManager() {
        return new LinearLayoutManager(getContext());
    }

    protected abstract ISectionFactory createViewFactory();

    protected abstract P createPresenter();

    @Override
    public void update(Object... args) {
        mProgressBar.setVisibility(View.VISIBLE);
        mRefreshLayout.setRefreshEnable(false);
    }

    @Override
    public void onRefresh() {
        mPresenter.refresh();
    }

    @Override
    public void updateData(List<SectionItem> items) {
        //加载完数据需调用该方法
        mRecyclerView.scrollToPosition(0);
        mProgressBar.setVisibility(View.GONE);
        mRefreshLayout.setVisibility(View.VISIBLE);
        mRefreshLayout.setRefreshing(false);
        mLineAdpater.setData(items);
        if (items == null || items.size() == 0) {
            mRefreshLayout.setRefreshEnable(false);
        } else {
            mRefreshLayout.setRefreshEnable(true);
        }
    }

    @Override
    public void updateDataAdd(List<SectionItem> items) {
        //加载完数据需调用该方法
        mRefreshLayout.setRefreshing(false);
        mLineAdpater.addData(items);
        footManger.setLoadMoreEnable(true);
    }

    /**
     * Whether need to load more
     *
     * @return
     */
    protected boolean createFootView() {
        return false;
    }

    @Override
    public void setFootStatus(int status, boolean showView) {
        post(() -> footManger.setState(status, showView));
    }

    @Override
    public void updateCount(BaseDataResult.PageInfo pageInfo) {

    }

    @Override
    public void errorData() {
        //加载完数据需调用该方法
        mRecyclerView.scrollToPosition(0);
        mProgressBar.setVisibility(View.GONE);
        mRefreshLayout.setVisibility(View.VISIBLE);
        mRefreshLayout.setRefreshing(false);
        mRefreshLayout.setRefreshEnable(false);
        mLineAdpater.errorData();
    }

    public void setEmptyView(View view) {
        mLineAdpater.setEmptyView(view);
    }

    public void scrollToTop() {
        if (mLineAdpater.getItemCount() > 0) {
            mRecyclerView.scrollToPosition(0);
        }
    }

    @Override
    public void showProgress(boolean show) {
        mProgressBar.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @Override
    public <T> LifecycleTransformer<T> getLifeTransformer() {
        return RxLifecycleAndroid.bindView(this);
    }
}
